package com.loongxun.xxapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loongxun.xxapp.R;
import com.loongxun.xxapp.model.User;

import java.util.List;

/**
 * Created by Administrator on 2017/10/27.
 */
public class MailAdapter extends BaseAdapter {

    private Context context;
    public List<User> list;
    private LayoutInflater inflater;

    public MailAdapter(Context context, List<User> list){
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<User> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_mail, null);

            holder.ll_item_user = (LinearLayout) convertView.findViewById(R.id.ll_item_user);
            holder.tv_item_name = (TextView) convertView.findViewById(R.id.tv_item_name);
            holder.tv_item_phone = (TextView) convertView.findViewById(R.id.tv_item_phone);
            holder.ll_item_call_phone = (LinearLayout) convertView.findViewById(R.id.ll_item_call_phone);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.ll_item_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list.get(position).getStart() == 0) {
                    list.get(position).setStart(1);
                }else {
                    list.get(position).setStart(0);
                }
                notifyDataSetChanged();
            }
        });

        holder.tv_item_name.setText(list.get(position).getUserName());
        if (list.get(position).getStart() == 0) {
            holder.tv_item_phone.setVisibility(View.GONE);
            holder.ll_item_call_phone.setVisibility(View.GONE);
        }else {
            holder.tv_item_phone.setVisibility(View.VISIBLE);
            holder.ll_item_call_phone.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    class ViewHolder {
        private TextView tv_item_name,tv_item_phone;
        private LinearLayout ll_item_user,ll_item_call_phone;
    }
}
